package gazzera.experimental;

import gazzera.gui.*;
import gazzera.gui.messages.*;
import gazzera.node.core.*;

import java.awt.event.*;
import java.io.*;

import javax.swing.*;


/**
 * @deprecated
 *
 */
public class Tester extends JFrame implements GuiInterface
{

	private Core core = null;
	private GuiInterface gui = null;
	private File keystore;
	private JTextArea txtLog;

	public Tester(File keystore, int port, int port2)
	{
		super("Tester");
		this.keystore = keystore;

		this.setSize(500, 300);

		final Tester t = this;

		// this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.addWindowListener(new WindowAdapter()
		{

			@Override
			public void windowClosing(WindowEvent e)
			{
				try
				{
					if (t.core != null)
						gui.pass(new Quit());
				}
				catch (Exception e1)
				{
					JOptionPane.showMessageDialog(t, e1.getMessage());
				}
				t.dispose();
			}

		});

		setLayout(null);
		final JTextField txtPort = new JTextField("" + port);
		txtPort.setBounds(10, 10, 100, 20);
		add(txtPort);

		JButton btnStart = new JButton("Start");
		btnStart.setBounds(120, 10, 60, 20);

		btnStart.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
					runCore(Integer.parseInt(txtPort.getText()));
				}
				catch (Exception e)
				{
					JOptionPane.showMessageDialog(t, e.getMessage());
				}
			}
		});
		add(btnStart);

		JButton btnStop = new JButton("Stop");
		btnStop.setBounds(190, 10, 60, 20);

		btnStop.addActionListener(new ActionListener()
		{

			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
				    	gui.pass(new Quit());
					core = null;
					setTitle("Stopped");
				}
				catch (Exception e)
				{
					JOptionPane.showMessageDialog(t, e.getMessage());
				}
			}
		});
		add(btnStop);

		final JTextField txtRemotePort = new JTextField("" + port2);
		txtRemotePort.setBounds(10, 50, 100, 20);
		add(txtRemotePort);
		JButton btnConnect = new JButton("Connect");
		btnConnect.setBounds(120, 50, 80, 20);

		btnConnect.addActionListener(new ActionListener()
		{
			@Override
			public void actionPerformed(ActionEvent arg0)
			{
				try
				{
				    gui.pass(new ConnectUrl("localhost", Integer.parseInt(txtRemotePort.getText())));
				}
				catch (Exception e)
				{
					JOptionPane.showMessageDialog(t, e.getMessage());
				}
			}
		});
		add(btnConnect);

		txtLog = new JTextArea();
		JScrollPane scrollingResult = new JScrollPane(txtLog);
		scrollingResult.setBounds(10, 80, 470, 190);
		add(scrollingResult);
		setVisible(true);
	}


	public void log(String s)
	{
		txtLog.append(s + "\n");
		txtLog.setCaretPosition(txtLog.getText().length());
	}

	public void runCore(int port) throws Exception
	{
		setTitle("Starting");
		core = new Core(keystore, this);
		gui = core;
		
		ThreadRunner.start(core, "Core", false);
		gui.pass(new Status());
	}

	@Override
	public void pass(GuiMessage msg)
	{
	    // here we receive message from the core
		if(msg instanceof Intro)
		{
			setTitle( ((Intro)msg).ourGid );
		}
		else if(msg instanceof Log)
		{
			log(((Log) msg).msg);
		}
		else
		{
			log(msg.toString());
		}
	}
	
	

}
